package com.example.day34.dao.impl;


import com.example.day34.dao.IUserDao;
import com.example.day34.model.PageInfos;
import com.example.day34.model.User;
import com.example.day34.rowmapper.UserRowmapper;
import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
@AllArgsConstructor
public class UserDaoImpl implements IUserDao {
    private final JdbcTemplate jdbcTemplate;
    @Override
    public List<User> list() {
        return  jdbcTemplate.query("select * from user1",new UserRowmapper());
    }

    @Override
    public int add(User user) {
        return jdbcTemplate.update("insert into user1 (name ,age,day) value (?,?,?)",
                user.getName(),
                user.getAge(),
                user.getDay()
        );
    }

    @Override
    public int upadte(User user) {
        return jdbcTemplate.update("update user1 set name=?,age=?,day=? where id=?",
                user.getName(),
                user.getAge(),
                user.getDay(),
                user.getId()
                );
    }

    @Override
    public int delete(int id) {
        return  jdbcTemplate.update("delete from user1 where id=?",id);
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        List<Object[]> list=new ArrayList<>();
        for (Integer id:ids) {
            list.add(new Object[]{id});
        }
        int numList[]=jdbcTemplate.batchUpdate("delete from user1 where id=?",list);
        int sum=0;
        for (int i:numList) {
            sum+=i;
        }
        return sum;
    }




}
